package com.springboot.framework.service.impl;

import com.springboot.framework.dao.entity.Shop;
import com.springboot.framework.dao.mapper.ShopMapper;
import com.springboot.framework.enums.Errors;
import com.springboot.framework.enums.StatusEnum;
import com.springboot.framework.exception.BusinessException;
import com.springboot.framework.model.ShopModel;
import com.springboot.framework.service.ShopService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author SWF
 * @Date 2019/6/6 13:20
 **/
@Service
public class ShopServiceImpl implements ShopService {

    @Resource
    private ShopMapper shopMapper;

    @Override
    public ShopModel get(Integer shopId) {
        Example example = new Example(Shop.class);
        example.and().andNotEqualTo("status",StatusEnum.DELETE.getCode())
                .andEqualTo("id",shopId);
       Shop shop = shopMapper.selectOneByExample(example);
       return convertShopModelFromShop(shop);
    }

    /**
     * 获取所有正常店铺
     */
    @Override
    public List<ShopModel> listNormal() {
        Example example = new Example(Shop.class);
        example.and().andEqualTo("status",StatusEnum.NORMAL.getCode());
        List<Shop> shops = shopMapper.selectByExample(example);

        return convertShopModelFromShop(shops);
    }

    @Override
    public List<ShopModel> listAll(){
        Example example = new Example(Shop.class);
        example.and().andNotEqualTo("status",StatusEnum.DELETE.getCode());
        List<Shop> shops = shopMapper.selectByExample(example);
        return convertShopModelFromShop(shops);
    }

    /**
     * 根据经纬度以及距离获取店铺
     * @param longitude
     * @param latitude
     * @param distance
     * @return
     */
    @Override
    public List<ShopModel> listByDistance(BigDecimal longitude, BigDecimal latitude, Integer distance) {
        List<Shop> shops = shopMapper.selectBydistance(longitude,latitude,distance);
        return convertShopModelFromShop(shops);
    }

    @Override
    public ShopModel checkShopStatus(Integer shopId) {
        ShopModel shopModel = this.get(shopId);
        if(shopModel == null){
            throw new BusinessException(Errors.SHOP_NOT_EXIST);
        }
        if(StatusEnum.DISABLED.getCode().equals(shopModel.getStatus())){
            throw new BusinessException(Errors.SHOP_HAS_DISABLED);
        }
        return shopModel;
    }

    /**
     * 新增店铺
     */
    @Override
    public ShopModel insert(ShopModel shopModel) {
        Shop shop = convertShopFromShopModel(shopModel);
        int resultCount = shopMapper.insertSelective(shop);
        if(resultCount == 0){
            throw new BusinessException(Errors.SYSTEM_INSERT_FAIL);
        }
        // TODO
        return convertShopModelFromShop(shop);
    }

    @Override
    public ShopModel update(ShopModel shopModel) {
        Shop shop = convertShopFromShopModel(shopModel);
        int resultCount = shopMapper.updateByPrimaryKeySelective(shop);
        if(resultCount == 0){
            throw new BusinessException(Errors.SYSTEM_INSERT_FAIL);
        }
        // TODO
        return convertShopModelFromShop(shop);
    }

    @Override
    public ShopModel updateStatus(ShopModel shopModel) {
        Shop shop = new Shop();
        shop.setId(shopModel.getId());
        shop.setStatus(shopModel.getStatus());
        int resultCount = shopMapper.updateByPrimaryKeySelective(shop);
        if(resultCount == 0){
            throw new BusinessException(Errors.SYSTEM_INSERT_FAIL);
        }
        // TODO
        return convertShopModelFromShop(shop);
    }


    private ShopModel convertShopModelFromShop(Shop shop){
        if(shop == null){
            return null;
        }
        ShopModel shopModel = new ShopModel();
        BeanUtils.copyProperties(shop,shopModel);
        return shopModel;
    }

    private List<ShopModel> convertShopModelFromShop(List<Shop> shops){
        if(shops == null){
            return null;
        }
        return shops.stream()
                .map( e -> convertShopModelFromShop(e))
                .collect(Collectors.toList());
    }

    private Shop convertShopFromShopModel(ShopModel shopModel){
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopModel,shop);
        return shop;
    }
}
